package com.wengu.algorithm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wengu.algorithm.dto.TaskAlg;
import com.wengu.algorithm.entity.AssignmentOutcome;
import com.wengu.algorithm.entity.AssignmentOutcomeTask;
import com.wengu.algorithm.entity.TaskArrivalSpatialDistribution;
import com.wengu.algorithm.mapper.AssignmentOutcomeTaskMapper;
import com.wengu.algorithm.service.AssignmentOutcomeTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wengu.algorithm.service.TaskArrivalSpatialDistributionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 单个任务模拟分配结果详情 服务实现类
 * </p>
 *
 * @author wanghailei
 * @since 2025-03-02
 */
@Slf4j
@Service
public class AssignmentOutcomeTaskServiceImpl extends ServiceImpl<AssignmentOutcomeTaskMapper, AssignmentOutcomeTask> implements AssignmentOutcomeTaskService {

    @Resource
    private TaskArrivalSpatialDistributionService taskService;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBatchOutcomeTask(AssignmentOutcome assignmentOutcome, Map<Long, TaskAlg> taskCacheMap) {
        List<TaskArrivalSpatialDistribution> taskArrivalSpatialDistributionList = taskService.list(Wrappers.<TaskArrivalSpatialDistribution>lambdaQuery().eq(TaskArrivalSpatialDistribution::getMainId, assignmentOutcome.getMainId()));
        List<AssignmentOutcomeTask> outcomeTaskList = new ArrayList<>();
        for (TaskArrivalSpatialDistribution taskArrivalSpatialDistribution : taskArrivalSpatialDistributionList) {
            AssignmentOutcomeTask assignmentOutcomeTask = new AssignmentOutcomeTask();
            assignmentOutcomeTask.setMainId(assignmentOutcome.getMainId());
            assignmentOutcomeTask.setOutcomeId(assignmentOutcome.getId());
            assignmentOutcomeTask.setVersionNo(assignmentOutcomeTask.getVersionNo());
            assignmentOutcomeTask.setTaskId(taskArrivalSpatialDistribution.getId());
            assignmentOutcomeTask.setCreateDate(new Date());
            assignmentOutcomeTask.setUpdateDate(new Date());
            assignmentOutcomeTask.setDeleteFlag(0);
            assignmentOutcomeTask.setExecutionCost(taskArrivalSpatialDistribution.getExecutionCost());
            TaskAlg taskCache = taskCacheMap.get(taskArrivalSpatialDistribution.getId());
            assignmentOutcomeTask.setTotalQuality(0d);
            assignmentOutcomeTask.setExecuteTimes(0);
            if (taskCache != null){
                try {
                    if (taskCache.fCache == null) {
                        assignmentOutcomeTask.setTotalQuality(0.5);
                    } else if(taskCache.fCache.get(taskCache.k) == null) {
                        assignmentOutcomeTask.setTotalQuality(Double.valueOf(taskCache.calculateF(taskCache.k).toString()));
                    } else {
                        assignmentOutcomeTask.setTotalQuality(Double.valueOf(taskCache.fCache.get(taskCache.k).toString()));
                    }
                } catch (Exception e) {
                    assignmentOutcomeTask.setTotalQuality(1.0);
                }
                assignmentOutcomeTask.setExecuteTimes(taskCache.getK());
            }
            outcomeTaskList.add(assignmentOutcomeTask);
        }
        this.saveBatch(outcomeTaskList);
    }
}
